//
//  LHUILabelAttributed.m
//  自带AlertView封装
//
//  Created by LH on 16/8/30.
//  Copyright © 2016年 LH. All rights reserved.
//

#import "LHUILabelAttributed.h"
#import <CoreText/CoreText.h>
@implementation LHUILabelAttributed
/**
 *  改变字体和改变字体的范围
 *  @param fontSize    设置的字体大小
 *  @param totalString 总的字符串
 *  @param startRange  开始位置
 *  @param lengthRange 改变长度
 *  @return 生成的富文本
 */
+(NSMutableAttributedString *)lh_changeFontAndFontSize:(NSInteger )fontSize TotalString:(NSString *)totalString StartRange:(NSInteger) startRange LengthRange:(NSInteger)lengthRange{
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    if (lengthRange+startRange<=totalString.length) {
        [attrStr addAttribute:NSFontAttributeName
                        value:[UIFont systemFontOfSize:fontSize]
                        range:NSMakeRange(startRange, lengthRange)];
    }

    return attrStr;
}
/**
 *  改变文字颜色和范围
 *  @param color       设置的文字颜色
 *  @param totalString 总的字符串
 *  @param startRange  开始位置
 *  @param lengthRange 改变长度
 *  @return 生成的富文本
 */
+(NSMutableAttributedString *)lh_changeFontAndColor:(UIColor *)color TotalString:(NSString *)totalString StartRange:(NSInteger) startRange LengthRange:(NSInteger)lengthRange{

    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    if (lengthRange+startRange<=totalString.length) {
        [attrStr addAttribute:NSForegroundColorAttributeName
                        value:color
                        range:NSMakeRange(startRange, lengthRange)];
    }
    return  attrStr;

}



/**
 *  单纯改变一句话中的某些字的颜色
 *
 *  @param color    需要改变成的颜色
 *  @param totalStr 总的字符串
 *  @param subArray 需要改变颜色的文字数组
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)lh_changeCorlorWithColor:(UIColor *)color TotalString:(NSString *)totalStr SubStringArray:(NSArray *)subArray {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
    for (NSString *rangeStr in subArray) {
        NSRange range = [totalStr rangeOfString:rangeStr options:NSBackwardsSearch];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    }
    return attributedStr;
}
/**
 *  改变某些文字的颜色 并单独设置其字体
 *
 *  @param fontSize    设置的字体
 *  @param color       颜色
 *  @param totalString 总的字符串
 *  @param subArray    想要变色的字符数组
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)lh_changeFontAndColorFontSize:(NSInteger)fontSize Color:(UIColor *)color TotalString:(NSString *)totalString SubStringArray:(NSArray *)subArray {
    
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    for (NSString *rangeStr in subArray) {
        NSRange range = [totalString rangeOfString:rangeStr options:NSBackwardsSearch];
        //[attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
        //[attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize] range:range];
        [attributedStr addAttributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} range:range];
    }
    return attributedStr;
}
/**
 *  添加下划线
 *  @param totalString 总的字符串
 *  @param type        下划线类型NSUnderlineStyle
 *  @param startRange  开始位置
 *  @param lengthRange 改变长度
 *  @return 生成的富文本
 */
+(NSMutableAttributedString *)lh_changeUnderlineAndTotalString:(NSString *)totalString LineType:(NSInteger)lineType StartRange:(NSInteger) startRange LengthRange:(NSInteger)lengthRange{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    if (lengthRange+startRange<=totalString.length) {
        [attrStr addAttribute:NSUnderlineStyleAttributeName
                        value:[NSNumber numberWithInteger:lineType]
                        range:NSMakeRange(startRange, lengthRange)];
        /*
         NSUnderlineStyleNone                                    =
         NSUnderlineStyleSingle                                  =
         NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0)      =
         NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0)     =
         NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0)      =
         NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0)        =
         NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0)       =
         NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0)    =
         NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) =
         */
    }
    return attrStr;
}
/**
 *  添加删除线
 *  @param totalString 总的字符串
 *  @param type        删除线类型NSUnderlineStyle
 *  @param startRange  开始位置
 *  @param lengthRange 改变长度
 *  @return 生成的富文本
 */
+(NSMutableAttributedString *)lh_changeStrikethroughAndTotalString:(NSString *)totalString LineType:(NSInteger)lineType LineColor:(UIColor *)lineColor StartRange:(NSInteger) startRange LengthRange:(NSInteger)lengthRange{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    [attributedStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid|NSUnderlineStyleSingle) range:NSMakeRange(startRange, lengthRange)];
    [attributedStr addAttribute:NSStrikethroughColorAttributeName value:lineColor range:NSMakeRange(startRange, lengthRange)];
    return attributedStr;
}
/**
 *  单纯改变句子的字间距（需要 <coretext coretext.h="">）
 *
 *  @param totalString 需要更改的字符串
 *  @param space       字间距
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)lh_changeSpaceWithTotalString:(NSString *)totalString Space:(CGFloat)space {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    long number = space;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    [attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
    CFRelease(num);
    return attributedStr;
}

/**
 *  单纯改变段落的行间距
 *
 *  @param totalString 需要更改的字符串
 *  @param lineSpace   行间距
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)lh_changeLineSpaceWithTotalString:(NSString *)totalString LineSpace:(CGFloat)lineSpace {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
    return attributedStr;
}

/**
 *  同时更改行间距和字间距
 *
 *  @param totalString 需要改变的字符串
 *  @param lineSpace   行间距
 *  @param textSpace   字间距
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)lh_changeLineAndTextSpaceWithTotalString:(NSString *)totalString LineSpace:(CGFloat)lineSpace textSpace:(CGFloat)textSpace {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
    long number = textSpace;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    [attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
    CFRelease(num);
    return attributedStr;
}
/**
 *  段落样式
 *
 *  @param totalString 需要改变的字符串
 *  @param lineSpace   行间距
 *  @param textSpace   段落间距
 *  @param firstIndent 指定段落开始的缩进像素
 *  @param headIndent  调整全部文字的缩进像素
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)lh_changeParagraphAndTotalString:(NSString *)totalString LineSpace:(CGFloat)lineSpace ParagraphSpacing:(CGFloat)paragraphSpacing FirstIndent:(CGFloat)firstIndent HeadIndent:(CGFloat)headIndent{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    //行间距
    paragraph.lineSpacing = lineSpace;
    //段落间距
    paragraph.paragraphSpacing = paragraphSpacing;
    //对齐方式
    paragraph.alignment = NSTextAlignmentLeft;
    //指定段落开始的缩进像素
    paragraph.firstLineHeadIndent = firstIndent;
    //调整全部文字的缩进像素
    paragraph.headIndent = headIndent;
    [attributedStr addAttribute:NSParagraphStyleAttributeName
                    value:paragraph
                    range:NSMakeRange(0, [totalString length])];
    return attributedStr;
}
@end
